

static int put_char(char c, FILE *stream); 
static int get_char(char c, FILE *stream); 
static FILE uart_str = FDEV_SETUP_STREAM(put_char, get_char, _FDEV_SETUP_RW); 

static int put_char(char c, FILE *stream) { 
   loop_until_bit_is_set(UCSRA, UDRE); // wait for UDR to be clear 
   UDR = c;    //send the character 
   return 0; 
} 

static int get_char(char c, FILE *stream) { 
   loop_until_bit_is_set(UCSRA, UDRE); // wait for UDR to be clear 
   c=UDR;    //send the character 
   return 0; 
} 

void init_uart(void) { 			// 8 bit, 1 stop bit, no parity
	UCSRB=0x18;
	UBRRL = 51;       //using an 16 Mhz crystal (19.2 baud)
	UCSRB |= (1 << RXEN) | (1 << TXEN) | (1 << RXCIE);   // Turn on the transmission and reception circuitry
   	UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes

//   	UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
  // 	UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
	stdout = &uart_str; 			//set the output stream 
} 
